<template>
  <div>
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #action="{ record, index }">
        <TableAction
          :actions="[
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record, index),
              },
              ifShow: record.canDel,
            },
          ]"
        />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, h, nextTick, watchEffect } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  import { columns, searchFormSchema } from './distributeTable.data';
  import { getListDistributeByFlow, getDelDistribute } from '/@/api/process/process';

  export default defineComponent({
    components: { BasicTable, TableAction },
    props: {
      typeCode: {
        type: String,
        default: '',
      },
      curFlowId: {
        type: [String, Number],
        default: '',
      },
    },
    setup(props, { emit }) {
      const { createMessage, createConfirm } = useMessage();

      watchEffect(async () => {
        nextTick(() => {
          if (props.curFlowId) {
            setProps({ searchInfo: { flowId: props.curFlowId } });
            reload();
          }
        });
      });

      const [registerTable, { reload, setTableData, deleteTableDataRecord, setProps }] = useTable({
        api: getListDistributeByFlow,
        title: '',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: true,
        showTableSetting: false,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        beforeFetch: (info) => {
          let newInfo = info;
          if (newInfo.dates && newInfo.dates.length > 0) {
            newInfo.fromDate = newInfo.dates[0];
            newInfo.toDate = newInfo.dates[1];
            delete newInfo.dates;
          }
          return newInfo;
        },
        immediate: false,
        pagination: false,
        canResize: false,
        rowKey: 'id',
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      //删除
      async function handleDelete(record) {
        await getDelDistribute({ id: record.id }).then(() => {
          createMessage.success(t('common.opSuccess'));
          reload();
        });
      }

      onMounted(() => {});
      return {
        registerTable,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
        handleDelete,
      };
    },
  });
</script>
